function scheduleHtmlParser(html) {
  courseInfos = []
  $("td[valign='top']").first().find('tr').each(function (tr_idx, tr) {
    if(tr_idx === 0) return
    $(tr).find('td').each(function (td_idx, td) {
      if(td_idx <= 1) return
      let s = $(td).text().trim()
      if( s === '&nbsp;' ) return
      console.log("解析之前的字符串：" + s)
      parseStringToData(s).forEach((course) => {
        //console.log("解析之后的结果：" + course.name + " " + course.position + " " + course.teacher + " " + course.day + " " + course.sections + " " + course.weeks)
        for(let i = 0; i < courseInfos.length; i++){
          if(equalTwoCourse(courseInfos[i],course)){            
            return
          }
        }
        courseInfos.push(course)
      })
    })
  })  

  sections = []
  $("td[align='left']").each(function (idx,td) {
    const section = {}
    const s = $(td).text().trim()
    const startAndEndTime = s.substring(s.lastIndexOf('&nbsp;'),s.length).replace(/&nbsp;/g, "").split('-')
    //console.log(startAndEndTime)
    section.section = idx + 1
    section.startTime = startAndEndTime[0]
    section.endTime = startAndEndTime[1]    
    sections.push(section)
  })
  console.log(sections)
  return {
    courseInfos: courseInfos, // 原先的返回内容
    something: sections, // 你自己定义的属性
  }
}


function parseStringToData(str) {
  var courses = str.split("课程:");
  courses = courses.filter((item) => item !== "");
  // for (let i = 0; i < courses.length; i++) {
  //   courses[i] = "课程:" + courses[i].replace(/&nbsp;/g, "");
  //   console.log("课程预处理：" + courses[i])
  // }

  const data = [];
  courses.forEach((course) => {
    const courseData = {};
    const name = course.split("班级:")[0].trim(); // 提取课程信息
    courseData.name = name.replace(" ", "");
    console.log("name: " + name)
    const details = course.split("班级:")[1].trim(); // 提取详细信息
    console.log("details: " + details)
    // 解析课程信息
    const position_regex = /\((.*?)\)/ // 正则表达式匹配括号内的数字和短横线

    const position = str.match(position_regex)[1]
    courseData.position = position
    console.log("position: " + position)
  
    courseData.teacher = details.split("主讲教师:")[1].trim();
    console.log("teacher: " + courseData.teacher)
    
    const weeks = extractWeeks(details)
    courseData.weeks = weeks
    console.log("weeks: " + weeks)

    const res = extractDayAndSection(details)    

    res.forEach((DayAndSection) => {
      courseData.day = DayAndSection.day
      courseData.sections = DayAndSection.sections
      data.push(Object.assign({}, courseData))
    })
  })
  return data;
}

function extractWeeks(str) {
  console.log(str)
  const regex = /(\d+-\d+)周/;
  const matches = str.match(regex);
  console.log(matches)
  if (matches) {
    const range = matches[1].split('-');
    const start = parseInt(range[0]);
    const end = parseInt(range[1]);
    var type = ''
    if(str.includes('单周')) {
      type = '单周'
    }else if(str.includes('双周')) {
      type = '双周'
    }
    const result = [];

    for (let i = start; i <= end; i++) {
      if (type === '单周' && i % 2 !== 0) {
        result.push(i);
      } else if (type === '双周' && i % 2 === 0) {
        result.push(i);
      } else if (!type) {
        result.push(i);
      }
    }

    return result;
  }

  return [];
}

function extractDayAndSection(str) {
  const regex = /星期([一二三四五六日])\s(\d+)-(\d+)节/g;
  let match;
  let results = [];

  while ((match = regex.exec(str)) !== null) {
    const day = match[1];
    const start = match[2];
    const end = match[3];
    const sections = generateNumArray(parseInt(start), parseInt(end))
    let dayNumber;    
    dayNumber = parseDayToNum(day)
    results.push({
      day: dayNumber,
      sections: sections
    });
  }
  if(results.length === 0) {
    ss = str.substring(str.indexOf('星期'),str.lastIndexOf('主讲教师')).split('星期')
    ss.forEach((s) => {
      if(s === '') return
      const day = parseDayToNum(s[0])
      const sections = []
      var idx = s.indexOf('上')
      while(idx !== -1 && idx+1 < s.length) {        
        sections.push(parseInt(s[idx + 1]))
        idx = s.indexOf('上',idx + 1)
      }
      results.push({
        day: day,
        sections: sections
      });
    })

  }
  return results;
}

function generateNumArray(start,end){
  let arr = []
  for(let i = start; i <= end; i++){
    arr.push(i)
  }
  return arr
}

function parseDayToNum(day){
  let dayNumber
  switch (day) {
    case '一':
      dayNumber = 1;
      break;
    case '二':
      dayNumber = 2;
      break;
    case '三':
      dayNumber = 3;
      break;
    case '四':
      dayNumber = 4;
      break;
    case '五':
      dayNumber = 5;
      break;
    case '六':
      dayNumber = 6;
      break;
    case '日':
      dayNumber = 7;
      break;
    default:
      dayNumber = -1; // 如果出现未知的星期几，则可以设置为其他值或抛出错误
  }
  return dayNumber
}

function equalTwoCourse(c1,c2){
  if(c1.name !== c2.name) return false
  if(c1.position !== c2.position) return false
  if(c1.teacher !== c2.teacher) return false
  if(c1.day !== c2.day) return false
  if(c1.sections.length !== c2.sections.length) return false
  for(let i = 0; i < c1.sections.length; i++){
    if(c1.sections[i] !== c2.sections[i]) return false
  }
  if(c1.weeks.length !== c2.weeks.length) return false
  for(let i = 0; i < c1.weeks.length; i++){
    if(c1.weeks[i] !== c2.weeks[i]) return false
  }
  return true
}